package com.example.myfirstapp;

import com.parse.ParseUser;
import com.parse.SignUpCallback;
import com.parse.ParseException;

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

public class SignUpFragment extends Fragment{
	@Override
	public View onCreateView( LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState )
	{
		View view = inflater.inflate( R.layout.frag_signup, container, false );
		
		Button buttonSignup = (Button) view.findViewById(R.id.button_signup);
		buttonSignup.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				signup(v);
			}
        });
		
		return view;
	}
	
    public void signup(View view){//view is a button
    	EditText editText = (EditText) getView().findViewById(R.id.edit_username);
    	String username = editText.getText().toString();
    	
    	editText = (EditText) getView().findViewById(R.id.edit_email);
    	String email = editText.getText().toString();
    	 
    	editText = (EditText) getView().findViewById(R.id.edit_password);
    	String password = editText.getText().toString();
    	
    	//TODO:first validate inputs here then continue

    	
//    	username = "gmertk";
//    	password = "12345";
//    	email = "mert@gmail.com";

    	ParseUser user = new ParseUser();
    	user.setUsername(username);
    	user.setPassword(password);
    	user.setEmail(email);
    	  
    	user.signUpInBackground(new SignUpCallback() {
    		public void done(ParseException e) {
    			if (e == null) {	
					Intent i = new Intent(getActivity(),HomeActivity.class);
					startActivity(i);
    			} 
    			else {
    	    		Log.e("signup error in parse", e.getCode()+"");
    			}
    		}
    	});    	
    }

}
